package servlet;

import util.DBUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.List;

public class EssayNewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("new");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int uid=Integer.parseInt(request.getParameter("uid"));
        String EssayTitle = request.getParameter("EssayTitle");
        String essay_class = request.getParameter("essay_class");
        String EssayLabel = request.getParameter("EssayLabel").replace(" ","");
        String content = request.getParameter("editorValue");
        if(EssayTitle.equals("")||essay_class==null||EssayLabel.equals("")||content==null){
            System.out.println("内容没有填写完整");
            request.getSession().setAttribute("msg","内容没有填写完整");
            request.getSession().setAttribute("status","0");
            RequestDispatcher rd = request.getRequestDispatcher("management.jsp");
            rd.forward(request,response);
        }else {
            System.out.println("EssayTitle:"+EssayTitle);
            System.out.println("uid:"+uid);
            System.out.println("essay_class:"+essay_class);
            System.out.println("EssayLabel:"+EssayLabel);
            System.out.println("content:"+content);
            String[] EssayLabel_new = EssayLabel.split("#");
            Timestamp createtime=new Timestamp(System.currentTimeMillis());
            try { Connection conn=null;
                PreparedStatement ptmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.cj.jdbc.Driver");//注册数据库驱动
                String url = "jdbc:mysql://localhost:3306/boke?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8";
                conn= DriverManager.getConnection(url, "root", "123456");//通过DriverManager获取数据库
                String sql="select * from essay where title=?";
                ptmt=conn.prepareStatement(sql);
                ptmt.setString(1, EssayTitle);
                System.out.println(ptmt);
                rs=ptmt.executeQuery();
                if(rs.next()){
                    System.out.println("文章标题已存在");
                    request.getSession().setAttribute("status","0");
                    request.getSession().setAttribute("msg","文章标题已存在");
                    RequestDispatcher rd = request.getRequestDispatcher("management.jsp");
                    rd.forward(request,response);
                }else {
                    String sql1="insert into essay(uid,eclass,create_time,content,read_sum,zan_sum,title) values(?,?,?,?,0,0,?)";
                    ptmt=conn.prepareStatement(sql1);
                    ptmt.setInt(1, uid);
                    ptmt.setString(2, essay_class);
                    ptmt.setTimestamp(3, createtime);
                    ptmt.setString(4, content);
                    ptmt.setString(5, EssayTitle);
                    System.out.println(ptmt);
                    int i=ptmt.executeUpdate();
                    if(i!=0) {

                        System.out.println("文章添加成功");
                        request.getSession().setAttribute("status","1");
                        request.getSession().setAttribute("msg","文章添加成功");

                        String sql2="select eid from essay where title=?";
                        ptmt=conn.prepareStatement(sql2);
                        ptmt.setString(1, EssayTitle);
                        rs=ptmt.executeQuery();
                        System.out.println(rs.toString());
                        int eid=0;
                        if(rs.next()){
                            eid=rs.getInt("eid");
                        }else {
                            System.out.println("eid搜索失败");
                        }

                        for (String o:EssayLabel_new) {
                            String sql4="select * from label where word=?";
                            ptmt=conn.prepareStatement(sql4);
                            ptmt.setString(1, o);
                            rs=ptmt.executeQuery();
                            System.out.println(rs.toString());
                            if(rs.next()){
                                if("".equals(o)){

                                }else {
                                String sql5="update label set count=count+1 where word=?";
                                ptmt=conn.prepareStatement(sql5);
                                ptmt.setString(1, o);
                                int a=ptmt.executeUpdate();
                                if(a!=0){
                                    System.out.println("关键词更新成功");
                                }else {
                                    System.out.println("关键词更新失败");
                                }
                                }
                            }else {
                                if("".equals(o)){

                                }else {
                                    String sql3="insert into label(word,count) values(?,1)";
                                    ptmt=conn.prepareStatement(sql3);
                                    ptmt.setString(1, o);
                                    System.out.println(ptmt);
                                    int a=ptmt.executeUpdate();
                                    if(a!=0){
                                        System.out.println("关键词添加成功");
                                    }else {
                                        System.out.println("关键词添加失败");
                                    }
                                }
                            }
                        }
                        for (String o:EssayLabel_new) {
                            if("".equals(o)){

                            }else {
                                String sql4 = "select lid from label where word=?";
                                ptmt = conn.prepareStatement(sql4);
                                ptmt.setString(1, o);
                                rs = ptmt.executeQuery();
                                if (rs.next()) {
                                    String sql6 = "insert into le(lid,eid) values(?,?)";
                                    ptmt = conn.prepareStatement(sql6);
                                    ptmt.setInt(1, rs.getInt("lid"));
                                    ptmt.setInt(2, eid);
                                    System.out.println(ptmt);
                                    int a=ptmt.executeUpdate();
                                } else {
                                    System.out.println("lid搜索失败");
                                }
                            }
                        }
                        String[] fields = {"eid","title","create_time","update_time","eclass","content"};
                        DBUtils db = new DBUtils();
                        String condition = "uid="+uid;
                        List<String[]> list1 = db.find("essay",fields,condition);
                        for (String[] arr : list1) {
                            System.out.printf("eid:%s,title:%s,create_time:%s,update_time:%s,eclass:%s,content:%s\n",arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]);
                        }
                        if(list1.isEmpty()){
                            System.out.println("信息加载失败");
                        }else {
                            request.getSession().setAttribute("essayData",list1);
                       /* request.getSession().setAttribute("status","1");
                        request.getSession().setAttribute("msg","信息加载成功");*/
                            RequestDispatcher rd = request.getRequestDispatcher("management.jsp");
                            rd.forward(request,response);
                        }
                    }else {
                        System.out.println("文章添加失败");
                        request.getSession().setAttribute("status","0");
                        request.getSession().setAttribute("msg","文章添加失败");
                        RequestDispatcher rd = request.getRequestDispatcher("management.jsp");
                        rd.forward(request,response);
                    }
                }

                rs.close();
                ptmt.close();
                conn.close();


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
